package com.example.leaderapplication.View.Activity.BanCi;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.leaderapplication.Adapter.BanCiListAdapter;
import com.example.leaderapplication.Base.BaseActivity;
import com.example.leaderapplication.Base.BaseConfig;
import com.example.leaderapplication.Bean.DeleteShiftsInfoRequsetBean;
import com.example.leaderapplication.Bean.DeleteShiftsInfoResultBean;
import com.example.leaderapplication.Bean.FindShiftsListRequsetBean;
import com.example.leaderapplication.Bean.FindShiftsListResultBean;
import com.example.leaderapplication.R;
import com.example.leaderapplication.RetrufitHelp.BaseSubscriber;
import com.example.leaderapplication.RetrufitHelp.ExceptionHandle;
import com.example.leaderapplication.RetrufitHelp.RetrofitClient;
import com.example.leaderapplication.Utils.SPUtil;
import com.example.leaderapplication.Utils.TimeUtil;
import com.example.leaderapplication.Utils.ToastUtil;
import com.example.leaderapplication.Widgets.DatePicker;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.wuhenzhizao.titlebar.widget.CommonTitleBar;

import java.util.ArrayList;
import java.util.List;

public class BanCiSettingActivity extends BaseActivity {

    private CommonTitleBar commonTitleBar;
    private LinearLayout zhiXinData;
    private TextView tvDataShow;
    private RecyclerView rvBanCi;
    private BanCiListAdapter banCiListAdapter;
    private RefreshLayout refreshLayout;
    private String businessOrgId;
    private FindShiftsListRequsetBean findShiftsListRequsetBean;
    private List<FindShiftsListResultBean.DataBean.DataListBean> BanCiData;
    private LinearLayout beginTime;
    private LinearLayout endTime;
    private String startDate;
    private String endtDate;
    private DeleteShiftsInfoRequsetBean deleteShiftsInfoRequsetBean;
    private int page;
    private int pageSize;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        commonTitleBar = findViewById(R.id.add_title_bar_ban_ci);
        commonTitleBar.setListener(new CommonTitleBar.OnTitleBarListener() {
            @Override
            public void onClicked(View v, int action, String extra) {
                if (action == CommonTitleBar.ACTION_LEFT_TEXT) {
                    finish();
                }
                if (action == CommonTitleBar.ACTION_RIGHT_TEXT) {
                    startActivity(new Intent(BanCiSettingActivity.this, AddBanCiActivity.class));
                }
            }
        });

    }

    @Override
    public int setLayoutId() {
        return R.layout.activity_ban_ci_setting;

    }

    @Override
    protected void initView() {

        //获取景区 businessOrgId 为请求
        businessOrgId = (String) SPUtil.get(BanCiSettingActivity.this, BaseConfig.BUSINESS_ORG_ID, "");

        if (BanCiData == null) {
            BanCiData = new ArrayList<>();
        }

        if (deleteShiftsInfoRequsetBean == null) {
            deleteShiftsInfoRequsetBean = new DeleteShiftsInfoRequsetBean();
        }

        beginTime = findViewById(R.id.ll_begin_action_time);
        endTime = findViewById(R.id.ll_end_action_time);

        zhiXinData = findViewById(R.id.ll_ban_ci_data);
        tvDataShow = findViewById(R.id.tv_ban_ci_data_show);
        rvBanCi = findViewById(R.id.rv_banci_show);
        refreshLayout = (RefreshLayout) findViewById(R.id.srl_home_page_reflsh);

        rvBanCi.setLayoutManager(new LinearLayoutManager(BanCiSettingActivity.this, RecyclerView.VERTICAL, false));
        banCiListAdapter = new BanCiListAdapter(BanCiData, BanCiSettingActivity.this);

        rvBanCi.setAdapter(banCiListAdapter);

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                // 这里做下拉刷新操作
                page = 1;
                pageSize = 5;
                findShiftsListRequsetBean.setPagesize(pageSize + "");
                findShiftsListRequsetBean.setPage(page + "");
                findShiftsListRequsetBean.setBusinessOrgId(businessOrgId);
                findShiftsList();
                refreshLayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
            }
        });

        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                // 这里做上拉刷新操作
                page = 1;
                pageSize = pageSize + 5;
                findShiftsListRequsetBean.setPagesize(pageSize + "");
                findShiftsListRequsetBean.setPage(page + "");
                findShiftsListRequsetBean.setBusinessOrgId(businessOrgId);
                findShiftsList();
                refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
            }
        });

        page = 1;
        pageSize = 5;
        findShiftsListRequsetBean = new FindShiftsListRequsetBean();
        findShiftsListRequsetBean.setPagesize(pageSize + "");
        findShiftsListRequsetBean.setPage(page + "");
        findShiftsListRequsetBean.setBusinessOrgId(businessOrgId);

        //获取班次信息
        findShiftsList();

    }

    private void findShiftsList() {
        RetrofitClient.getInstance(BanCiSettingActivity.this, baseUrl, headers).findShiftsList(findShiftsListRequsetBean, new BaseSubscriber<FindShiftsListResultBean>(BanCiSettingActivity.this) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable e) {
                ToastUtil.showToast(e.message);
            }

            @Override
            public void onNext(FindShiftsListResultBean findShiftsListResultBean) {
                if (findShiftsListResultBean.getCode().equals("200")) {
                    BanCiData.clear();
                    BanCiData.addAll(findShiftsListResultBean.getData().getDataList());
                    banCiListAdapter.notifyDataSetChanged();
                } else {
                    ToastUtil.showToast(findShiftsListResultBean.getMessage());
                }

            }
        });
    }

    @Override
    protected void doSomeThing() {

        beginTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showdata("begin");
            }
        });

        endTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (startDate != null) {
                    showdata("end");
                } else {
                    ToastUtil.showToast("请先设置开始时间");
                }
            }
        });

        // 条目点击事件    详情，   修改，   删除
        banCiListAdapter.setShipMessageListener(new BanCiListAdapter.ShipMessageListener() {
            @Override
            public void shipMessage(int postion) {
                Intent intent = new Intent(BanCiSettingActivity.this, ShowDetailBanciActivity.class);
                intent.putExtra("shiftsId", BanCiData.get(postion).getShiftsId());
                startActivity(intent);
            }
        });

        banCiListAdapter.setChangeListener(new BanCiListAdapter.ChangeListener() {
            @Override
            public void itemChange(int position) {
                Intent intent = new Intent(BanCiSettingActivity.this, ChangeBanCiActivity.class);
                intent.putExtra("shiftsId", BanCiData.get(position).getShiftsId());
                startActivity(intent);
            }
        });

        banCiListAdapter.setDeleteListener(new BanCiListAdapter.DeleteListener() {
            @Override
            public void itemDelete(int position) {
                deleteShiftsInfoRequsetBean.setShiftsId(BanCiData.get(position).getShiftsId());
                deleteShiftsInfo(position);
            }
        });

    }

    private void deleteShiftsInfo(int position) {
        RetrofitClient.getInstance(BanCiSettingActivity.this, baseUrl, headers).deleteShiftsInfo(deleteShiftsInfoRequsetBean, new BaseSubscriber<DeleteShiftsInfoResultBean>(BanCiSettingActivity.this) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable e) {

                ToastUtil.showToast(e.message);
            }

            @Override
            public void onNext(DeleteShiftsInfoResultBean deleteShiftsInfoResultBean) {
                ToastUtil.showToast(deleteShiftsInfoResultBean.getMessage());
                BanCiData.remove(position);
                banCiListAdapter.notifyDataSetChanged();
            }
        });
    }

    private void showdata(String from) {

        DatePicker datePicker = new DatePicker(BanCiSettingActivity.this);

        datePicker.setOnCancelClickListener(view1 -> datePicker.dismiss())
                //“确定”的点击事件
                .setOnConfirmClickListener(view12 -> {
                    if (from.equals("begin")) {
                        startDate = datePicker.getSelectedYear() + "-" + datePicker.getSelectedMonth() + "-" + datePicker.getSelectedDay();
                        tvDataShow.setText("执行日期：" + startDate + "至     待定");
                        findShiftsListRequsetBean.setStartDate(startDate);
                    } else {
                        endtDate = datePicker.getSelectedYear() + "-" + datePicker.getSelectedMonth() + "-" + datePicker.getSelectedDay();
                        //对比时间，开始时间要比结束时间早
                        if (TimeUtil.compareTime(startDate, endtDate)) {
                            tvDataShow.setText("执行日期：" + startDate + "至 " + endtDate);
                            findShiftsListRequsetBean.setEndDate(endtDate);
                            //获取班次信息  当第二个日期设置完成，那么就去从新请求
                            findShiftsList();
                        } else {
                            ToastUtil.showToast("结束时间应大于开始时间，请重新设置");
                        }


                    }

                    datePicker.dismiss();
                })
                //“取消”、“确定”的文字颜色
                .setColorConfirm(Color.BLUE)
                .setColorCancel(Color.BLACK)

                //选择“时间”的显示样式
                .setDateShowStyle(DatePicker.DATE_SHOW_STYLE_3)

                // 左右箭头颜色
                .setColorLeftArrow(Color.DKGRAY)
                .setColorRightArrow(Color.DKGRAY)

                //星期的颜色
                .setColorWeeks(DatePicker.SUNDAY, Color.DKGRAY)

                //“今天”以前、今天、“今天”以后 的显示文字颜色
                .setColorFontBeforeToday(Color.LTGRAY)
                .setColorFontToday(Color.WHITE)
                .setColorFontAfterToday(Color.BLACK)

                //“今天”的背景颜色
                .setColorBgdToday(Color.GRAY)
                //选中日期的文字颜色和背景颜色
                .setColorFontSelectedDay(Color.WHITE)
                .setColorBgdSelectedDay(Color.DKGRAY)
                .show();

    }
}
